package org.tourism.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.tourism.entity.AttractionReview;
import org.tourism.service.AttractionReviewService;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuling
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/AttractionReview")
public class AttractionReviewController {

    @Autowired
    private AttractionReviewService attractionReviewService;

    // 查询所有AttractionReview记录
    @GetMapping("/getall")
    public ResponseEntity<List<AttractionReview>> getAllAttractionReviews() {
        List<AttractionReview> attractionReviews = attractionReviewService.list();
        return ResponseEntity.ok(attractionReviews);
    }

    // 根据id查询AttractionReview记录
    @GetMapping("/{id}")
    public ResponseEntity<AttractionReview> getAttractionReviewById(@PathVariable Long id) {
        AttractionReview attractionReview = attractionReviewService.getById(id);
        return attractionReview != null ? ResponseEntity.ok(attractionReview) : ResponseEntity.notFound().build();
    }

    // 新增AttractionReview记录
    @PostMapping("/add")
    public ResponseEntity<String> addAttractionReview(@RequestBody AttractionReview attractionReview) {
        boolean saved = attractionReviewService.save(attractionReview);
        return saved ? ResponseEntity.ok("Saved successfully") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Save failed");
    }

    // 更新AttractionReview记录
    @PutMapping("/{id}")
    public ResponseEntity<String> updateAttractionReview(@PathVariable Long id, @RequestBody AttractionReview attractionReview) {
        attractionReview.setId(id);
        boolean updated = attractionReviewService.updateById(attractionReview);
        return updated ? ResponseEntity.ok("Updated successfully") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Update failed");
    }

    // 删除AttractionReview记录
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteAttractionReview(@PathVariable Long id) {
        boolean deleted = attractionReviewService.removeById(id);
        return deleted ? ResponseEntity.ok("Deleted successfully") : ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Delete failed");
    }
}
